package com.itheima.health.utils.listener;

import com.itheima.health.utils.jwt.UserInfo;
import com.itheima.health.utils.redis.DistributedRedisLock;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.UserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * spring ioc容器初始化完成后自动执行
 * 一般用于项目初始化
 */
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

    @Resource
    private RedisTemplate<Object,Object> redisTemplate;

    @Resource
    private DistributedRedisLock distributedRedisLock;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("=========redis-------");
        RedisUtil.register(redisTemplate);
        RedisUtil.registerLock(distributedRedisLock);

        //  初始化 用户账号和权限信息
        //   初始化 工作
        UserInfo userInfo = new UserInfo(1L,"tom","123","save-ROLE_QUERY-delete-update");
        UserInfo userInfo1 = new UserInfo(2L,"marry","123","find-delete");
        RedisUtil.set(UserConstant.SYSUSERINFO +userInfo.getUsername(),userInfo);
        RedisUtil.set(UserConstant.SYSUSERINFO +userInfo1.getUsername(),userInfo1);


    }
}
